import 'package:flutter/material.dart';
import 'package:flutter_deer/res/colors.dart';
import 'package:flutter_deer/res/constant.dart';
import 'package:flutter_deer/ui/setting/setting_item_data.dart';
import 'package:flutter_deer/util/print/data/print_link_type.dart';
import 'package:flutter_deer/util/print/print_manager.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';

class SettingPage3LinkSelect extends StatefulWidget {
  const SettingPage3LinkSelect({super.key});

  @override
  State<SettingPage3LinkSelect> createState() => _SettingPage3LinkSelectState();
}

class _SettingPage3LinkSelectState extends State<SettingPage3LinkSelect> {
  final _mPrinter = Get.find<PrintEngineManager>();
  final Rx<PrintLinkType> _printLinkType = PrintLinkType.values[0].obs;

  // 选项数据
  final List<Map<String, String>> _options = [
    {'title': '不打印'},
    {'title': '客户联'},
    {'title': '商户联'},
    {'title': '两联'},
  ];

  @override
  Widget build(BuildContext context) {
    // 平板布局 - 左右分栏
    return Scaffold(
      backgroundColor: Colours.bg_color,
      body: Obx(() => Row(
            children: [
              // 左侧菜单区域
              Container(
                width: 280,
                color: Colours.bg_color,
                padding: const EdgeInsets.only(top: 8.0, bottom: 8.0, left: 0.0, right: 8.0),
                child: ListView(
                  children: <Widget>[
                    // 语音打印和打印控制组合项
                    _buildSettingGroup(_mPrinter.rxPrintLinkType
                        .map((item) => SettingItemData(
                              title: item.dec,
                              trailing: Text(PrintCouplet.getPrintCouplet(item.linkType).valueCode, style: const TextStyle(fontSize: 12, color: Colors.grey)),
                              showArrow: true,
                              onTap: () => _printLinkType.value = item,
                            ))
                        .toList()),
                  ],
                ),
              ),

              // 右侧内容区域
              Expanded(
                child: Container(
                    padding: const EdgeInsets.only(top: 8.0, bottom: 18.0, left: 0.0, right: 8.0),
                    child: Align(
                      alignment: Alignment.topCenter,
                      child: Container(
                        color: Colours.white,
                        padding: const EdgeInsets.only(top: 8.0, bottom: 8.0, left: 8.0, right: 8.0),
                        child: Column(
                          mainAxisSize: MainAxisSize.min,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Center(
                              child: Text('${_printLinkType.value.dec}', style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: Colors.black87)),
                            ),
                            const SizedBox(height: 8),
                            _buildRadioListTile(0),
                            _buildRadioListTile(1),
                            _buildRadioListTile(2),
                            _buildRadioListTile(3),
                          ],
                        ),
                      ),
                    )),
              ),
            ],
          )),
    );
  }

  // 构建设置项组
  Widget _buildSettingGroup(List<SettingItemData> items) {
    return Material(
      color: Colors.white,
      borderRadius: BorderRadius.circular(3),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: items.map((item) {
          final int index = items.indexOf(item);
          final bool isSelected = _printLinkType.value.dec == item.title;
          return Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Material(
                  color: isSelected ? Theme.of(context).primaryColor.withOpacity(0.1) : Colors.transparent,
                  child: InkWell(
                    hoverColor: Colours.bg_press,
                    onTap: item.onTap,
                    child: Container(
                      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
                      child: Row(
                        children: [
                          Expanded(child: Text(item.title, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: Colors.black87))),
                          if (item.trailing != null) item.trailing!,
                          if (item.showArrow) const Icon(Icons.arrow_forward_ios, color: Colors.grey, size: 14),
                        ],
                      ),
                    ),
                  )),
              if (index < items.length - 1) const Divider(height: 1, thickness: 1, indent: 16, endIndent: 16),
            ],
          );
        }).toList(),
      ),
    );
  }

  // 构建单选列表项
  Widget _buildRadioListTile(int index) {
    final option = _options[index];
    final bool isSelected = _printLinkType.value.linkType == index;
    return Material(
      color: isSelected ? Theme.of(context).primaryColor.withOpacity(0.1) : Colors.transparent,
      child: InkWell(
        onTap: () {
          _printLinkType.value.linkType = index;
          _printLinkType.refresh();
          SpUtil.putObjectList(Constant.optionSwitchPrintLinkType, _mPrinter.rxPrintLinkType.map((e) => e.toJson()).toList());
        },
        child: Container(
          padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
          child: Row(
            children: [
              Expanded(
                child: Text(
                  option['title']!,
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
                    color: isSelected ? Theme.of(context).primaryColor : Colors.black87,
                  ),
                ),
              ),
              Radio<int>(
                value: index, // 当前选项的值
                groupValue: _printLinkType.value.linkType, // 当前选中的值
                onChanged: (value) {
                  _printLinkType.value.linkType = index;
                  _printLinkType.refresh();
                  SpUtil.putObjectList(Constant.optionSwitchPrintLinkType, _mPrinter.rxPrintLinkType.map((e) => e.toJson()).toList());
                },
                activeColor: Theme.of(context).primaryColor,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
